package it.prova.transform;

import it.prova.Consts;
import it.prova.Utility;

import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.AbstractTransformOperation;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class EntryUnitIterateTransformOperation extends AbstractTransformOperation
{	
	private int iterationNumber = -1;
	
	public EntryUnitIterateTransformOperation()
	{
		super();
	}
	
	public Element transform(Element sourceElement, Element displayElement)
	{
		if(tagConverterContext == null)
		{
			return displayElement;
		}
		
		Element firstElement = null;
		if(sourceElement.getParentNode() != null)
		{
			String parent = sourceElement.getParentNode().getLocalName();
			
			if(parent != null)
			{
				if(Consts.TAG_TR_NAME.equals(parent))
				{
					firstElement = tagConverterContext.createElement("td");
				}
				else if(Consts.TAG_TABLE_NAME.equals(parent))
				{
					firstElement = tagConverterContext.createElement("tbody");
//					firstElement.appendChild(tagConverterContext.createText("pippo"));
//					firstElement.setAttribute("style", "color:red;");
//					firstElement.setAttribute("class", "tableClass");
				}
				else if(Consts.TAG_TABLE_NAME.equals(parent))
				{
					
				}
				else
				{
					firstElement = tagConverterContext.createElement("span");
				}
			}
			else
			{
				firstElement = tagConverterContext.createElement("span");
			}
		}
		else
		{
			firstElement = tagConverterContext.createElement("span");
		}
		
		String number = getIterationNumber(sourceElement);
		
		String text = "";
		if(iterationNumber < 0)
		{
			text +=	"<" + sourceElement.getPrefix() + ":" +
					sourceElement.getLocalName() + " " + 
					Consts.NUMBER_ATTRIBUTE_NAME + "=\"" + 
					number + "\" />";
			firstElement.appendChild(tagConverterContext.createText(text));
		}
		else if(iterationNumber > 0)
		{
			int outputType = 0;
			
			switch(outputType)
			{
				case 0:
					copyChildren(sourceElement, firstElement);
					break;
				case 1:
					parseChildren(sourceElement, firstElement);
					break;
			}
		}
		else
		{
			firstElement.appendChild(tagConverterContext.createText(text));
		}
		
//		nu.xom.Element child = DOMConverter.convert(e);
//		System.out.println("child:");
//		System.out.println(child.toXML());
		print(firstElement, "");
		
		return firstElement;
	}
	
	private String getIterationNumber(Element element)
	{
		String number = element.getAttribute(Consts.NUMBER_ATTRIBUTE_NAME);
		
		if(number != null)
		{
			try
			{
				iterationNumber = Integer.parseInt(number);
			}
			catch(NumberFormatException nfe)
			{
				number = "";
			}
		}
		else
		{
			number = "";
		}
		return number;
	}
	
	private void copyChildren(Element sourceElement, Element firstElement)
	{
//		if(Consts.TAG_TABLE_NAME.equals(firstElement.getLocalName()))
//		{
			for(int i=0; i<iterationNumber; i++)
			{	
//				for(int j=0; j<sourceElement.getChildNodes().getLength(); j++)
//				{					
//					Node child = sourceElement.getChildNodes().item(j);
//					
//					if(child instanceof Element)	
//					{
//						tagConverterContext.copyChildren(firstElement, (Element) child);
//					}
//					
//					Node newChild = child.cloneNode(true);
//					
//					if(newChild instanceof Element)
//					{
//						firstElement.appendChild(newChild);
//						
//						if(Consts.TAG_TR_NAME.equals(newChild.getLocalName()))
//						{
//							NodeList trChildren = newChild.getChildNodes();
//							for(int x=0; x<trChildren.getLength(); x++)
//							{
//								if(trChildren.item(x) instanceof Element)
//								{
//									Element el = (Element) trChildren.item(x);
//									if
//									(
//										Consts.TAG_LABEL_NAME.equals(el.getLocalName()) || 
//										Consts.TAG_VALUE_NAME.equals(el.getLocalName())
//									)
//									{
//										MultiDataUnitTransformOperation transform = new MultiDataUnitTransformOperation();
//										transform.setTagConverterContext(tagConverterContext);
//										el = transform.transform(el, el);
//									}
//									else
//									{
//										Utility.transformAllElementsByTagName(el, Consts.TAG_LABEL_NAME, tagConverterContext);
//										Utility.transformAllElementsByTagName(el, Consts.TAG_VALUE_NAME, tagConverterContext);
//									}
//									firstElement.appendChild(el);
//								}
//							}
//						}
//						else
//						{
//							firstElement.appendChild(newChild);
//						}
//					}
//				}
				//Element parent = (Element)sourceElement.getParentNode();
				//tagConverterContext.copyChildren(firstElement, parent);
				tagConverterContext.copyChildren(sourceElement, firstElement);
			}
//		}
//		else
//		{
//			for(int i=0; i<iterationNumber; i++)
//			{
//				tagConverterContext.copyChildren(sourceElement, firstElement);
//			}
//		}
	}
	
	private void parseChildren(Element sourceElement, Element firstElement)
	{
		System.out.println("ChildNodesNumber = " + sourceElement.getChildNodes().getLength());
		for(int j=0; j<sourceElement.getChildNodes().getLength(); j++)
		{
			Node child = sourceElement.getChildNodes().item(j);
			
//			if(child instanceof Element)	
//			{
//				tagConverterContext.copyChildren(firstElement, (Element) child);
//			}
		
			Node newChild = child.cloneNode(true);
			
			if(newChild instanceof Element)
			{
				Element elem = (Element) newChild;
				
				if
				(
					Consts.TAG_LABEL_NAME.equals(elem.getLocalName()) || 
					Consts.TAG_VALUE_NAME.equals(elem.getLocalName())
				)
			    {
					MultiDataUnitTransformOperation transform = new MultiDataUnitTransformOperation();
					transform.setTagConverterContext(tagConverterContext);
					org.w3c.dom.Element newElement = transform.transform(elem, elem);
					firstElement.appendChild(newElement);
			    }
				else
				{
					Utility.transformAllElementsByTagName(elem, Consts.TAG_LABEL_NAME, tagConverterContext);
					Utility.transformAllElementsByTagName(elem, Consts.TAG_VALUE_NAME, tagConverterContext);
				}
			}
			firstElement.appendChild(newChild);
		}
		tagConverterContext.copyChildren(sourceElement, firstElement);
	}
	
	private void print(Node node, String start)
	{
		System.out.println("node = " + start + " " + node.getLocalName());
		
		for(int i=0; i<node.getChildNodes().getLength(); i++)
		{
			print(node.getChildNodes().item(i), start + "***");
		}
	}
}